package wechat.chinese.weixin;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.sf.json.JSONObject;

import org.apache.http.protocol.HTTP;

import cj.utils.Constants;
import cj.utils.HttpClientUtil;

public class AccessToken implements ServletContextListener {
	public static final String YUWEN_APPID = "wx48e41de45aeedb18";
	public static final String YUWEN_APPSECRET = "6adbb933117352bff169caac2f36ca23";
	public static String accessToken = null;
	public void contextInitialized(ServletContextEvent sce) {
//		new Thread(new TokenThread()).start();
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}
	
	

}

class TokenThread implements Runnable {
	
	public void run(){
		while(true){
			try{
				Thread.sleep(5 * 1000);
				refreshAccessToken();
				Thread.sleep(7000 * 1000);
			}
			catch(Exception e){
				
			}
			
		}
	}

	private void refreshAccessToken() {
		String json = HttpClientUtil.get(Constants.weixinkey.weixinUrl+"&appid="+AccessToken.YUWEN_APPID
				+"&secret="+AccessToken.YUWEN_APPSECRET, HTTP.UTF_8);
		JSONObject jsonObject = JSONObject.fromObject(json); 
		AccessToken.accessToken = jsonObject.get("access_token").toString();
	}
}
